/**
 * Created by forcht on 2018/5/19.
 */
public class Solution {
    public int maxProfit(int[] prices) {
        int buy1=Integer.MIN_VALUE,buy2=Integer.MIN_VALUE,sell1=0,sell2=0;
        for(int i=0;i<prices.length;i++){
            buy1=Math.max(buy1,-prices[i]);   //第一次买股票花费的钱
            sell1=Math.max(sell1,prices[i]+buy1); //第一次卖股票赚的钱

            buy2=Math.max(buy2,sell1-prices[i]); //第二次买完股票后剩余的钱
            sell2=Math.max(sell2,buy2+prices[i]);//两次交易后赚的钱
        }
        return sell2;
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        int[] a={2,1,3,5,1};
        System.out.println(solution.maxProfit(a));
    }
}
